//
//  NSObject+EBPropertyListing.m
//  iDiscover
//
//  Created by embraiz on 15/5/15.
//  Copyright (c) 2015年 EmBraiz. All rights reserved.
//

#import "NSObject+EBPropertyListing.h"

@implementation NSObject (EBPropertyListing)
/* 获取对象的所有属性 */
//若NSString为NSNull，将NSNull转化为@""

- (void)fixedString
{
    
    NSMutableDictionary *props = [NSMutableDictionary dictionary];
    
    unsigned int outCount, i;
    
    //获取所有属性
    objc_property_t *properties = class_copyPropertyList([self class], &outCount);
    //遍历属性
    for (i = 0; i<outCount; i++)
        
    {
        objc_property_t property = properties[i];
        
        const char* char_f = property_getName(property);//const char -- key
        
        NSString *propertyName = [NSString stringWithUTF8String:char_f];// NSString -- key
        
        id propertyValue = [self valueForKey:(NSString *)propertyName];//value for key(NSString)
        
        //------ type ------
        const char* char_a = property_getAttributes(property);
        
        NSString *propertyType = [NSString stringWithUTF8String:char_a];
        
        NSString *type = [[propertyType componentsSeparatedByString:@","] firstObject];
        
        if (([propertyValue isMemberOfClass:[NSNull class]]||!propertyValue)&&[@"T@\"NSString\"" isEqualToString:type]) {
            [self setValue:@"" forKey:propertyName];
        }
        
        if (propertyValue) [props setObject:propertyValue forKey:propertyName];
    }
    
    free(properties);
}

@end
